﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Ext.Net;
using BLL;
using Model;
using System.Collections.Generic;

namespace Web.View.roleMgr
{
    public partial class Default : UIPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void InfoBind(object sender, StoreRefreshDataEventArgs e)
        {
            rolesMgr mgr = new rolesMgr();
            IList<roles> list = mgr.FindAll_roles();
            role_Store.DataSource = list;
            role_Store.DataBind();
        }
        protected void HandleChanges(object sender, BeforeStoreChangedEventArgs e)
        {
            rolesMgr mgr = new rolesMgr();
            ChangeRecords<roles> persons = e.DataHandler.ObjectData<roles>();
            foreach (roles updated in persons.Updated)
            {
                if (updated.rolename.Trim().Length <= 0) continue;
                try
                {
                    if (mgr.Update_roles(updated))
                        X.Msg.Notify("成功", "<font color='green'>角色信息更新成功！</font>").Show();
                }
                catch
                {
                    X.Msg.Notify("失败", "<font color='red'>角色信息更新失败!</font>").Show();
                }
            }
            foreach (roles inserted in persons.Created)
            {
                if (inserted.rolename.Trim().Length <= 0) continue;
                try
                {
                    inserted.roleno = (DateTime.Now.ToString("yyMMddHHmmss")).ToString();
                    if (mgr.Add_roles(inserted))
                        X.Msg.Notify("成功", "<font color='green'>角色添加成功！</font>").Show();
                }
                catch { X.Msg.Notify("失败", "<font color='red'>角色添加失败!</font>").Show(); }
            }
            e.Cancel = true;
        }
        //删除权限
        protected void BtnDel_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.role_Grid.SelectionModel.Primary as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                roles user = new roles();
                user.roleid = int.Parse(row.RecordID);

                if (row.RecordID == Session["role"].ToString())
                {
                    X.Msg.Notify("失败", "<font color='red'>系统不允许删除超级管理员的角色权限!</font>").Show();
                    continue;
                }
                rolesMgr mgr = new rolesMgr();
                rolefunMgr funmgr = new rolefunMgr();
                if (mgr.Del_roles(user))
                {
                    funmgr.DelAllRoleFun(row.RecordID);
                    X.Msg.Notify("成功", "<font color='green'>已经成功删除选中的角色</font>").Show();
                }
                else
                    X.Msg.Notify("失败", "<font color='Red'>删除失败，请您重试！</font>").Show();
            }
            role_Grid.Reload();
        }
        /// <summary>
        /// 获取功能菜单树
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GetExamplesNodes(object sender, NodeLoadEventArgs e)
        {
            if (e.NodeID == "root")
            {
                rolefunMgr rolefunMgr = new rolefunMgr();
                e.Nodes = rolefunMgr.GetMenu("-1");
            }
        }

        //双击Grid的列，弹出修改框
        protected void roleForFunGridDBClick(object sender, DirectEventArgs e)
        {
            rolefunMgr mgr = new rolefunMgr();
            rolefun_Store.DataSource = mgr.GetRoleMenu(e.ExtraParams["id"].ToString());
            rolefun_Store.DataBind();
            //funOfrole_Grid.Reload();
        }

        protected void AllFunBind(object sender, StoreRefreshDataEventArgs e)
        {
            userfunMgr mgr = new userfunMgr();
            AllFun_Store.DataSource = mgr.FindAll_userfun();
            AllFun_Store.DataBind();
        }

        protected void BtnGiveFuns_Click(object sender, DirectEventArgs e)
        {
            rolefunMgr mgr = new rolefunMgr();
            if (mgr.roleFunGive(e.ExtraParams["id"].ToString()))
            {
                funOfrole_Grid.Reload();
                X.Msg.Notify("成功", "<font color='green'>恭喜您，授权成功</font>").Show();
            }else
                X.Msg.Notify("失败", "<font color='red'>授权失败，请您重试</font>").Show();
        }

        protected void BtnDelFuns_Click(object sender, DirectEventArgs e)
        {
            rolefunMgr mgr=new rolefunMgr();

            RowSelectionModel sm = this.funOfrole_Grid.SelectionModel.Primary as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                rolefun rolefun=new Model.rolefun();
                rolefun.pid=int.Parse(row.RecordID);
                mgr.Del_rolefun(rolefun);
            }
            X.Msg.Notify("成功", "<font color='green'>恭喜您，功能菜单删除成功！</font>").Show();
        }
        protected void BtnChangeStatic_Click(object sender, DirectEventArgs e)
        {
            rolefunMgr mgr = new rolefunMgr();
            RowSelectionModel sm = this.funOfrole_Grid.SelectionModel.Primary as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                rolefun fun = mgr.FindById_rolefun(int.Parse(row.RecordID));
                if (fun.rolep == 0)
                    fun.rolep = 1;
                else
                    fun.rolep = 0;
                mgr.Update_rolefun(fun);
            }
            X.Msg.Notify("成功", "<font color='green'>恭喜您，读写状态切换成功！</font>").Show();
        }
    }
}
